﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Globalization;
using System.Resources;
using System.Threading;
using Telerik.WinControls.UI.Data;

namespace CRMG.Container.Views.InterViews
{
    public partial class FrmInterView : Telerik.WinControls.UI.RadForm
    {

        ResourceManager formRes;
        ResourceManager messageRes;
        InterViewManager intrVwMan;
        StudentManager stdntMan;
        InterView pInterView;

        int stdntId = -1;

        public FrmInterView()
            : this(-1)
        { }
        public FrmInterView(int studentId)
        {
            try
            {
                InitializeComponent();
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(AppVariables.ApplicationCulture);
                intrVwMan = new InterViewManager();
                stdntMan = new StudentManager();
                stdntId = studentId;
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmInterView).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmInterView).Assembly);
                dtpTime.Culture = Thread.CurrentThread.CurrentUICulture;
                dtpTime.Value = DateTime.Now;
                lblStudent.Text = formRes.GetString("FrmInterView.lblStudent");
                lblTime.Text = formRes.GetString("FrmInterView.lblTime");
                lblRemarks.Text = formRes.GetString("FrmInterView.lblRemarks");
                lblComments.Text = formRes.GetString("FrmInterView.lblComments");
                btnSave.Text = formRes.GetString("Save");
                btnCancel.Text = formRes.GetString("Cancel");

            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmInterView.Ctor.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2012,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2012)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmInterView.Ctor.Error"),
                    messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = stdntId != -1;
            }
        }

        private void FormLoad(object sender, EventArgs e)
        {
            try
            {
                pInterView = new InterView();
                ControlUtil.LoadDataTable2DropDownList(drpStudent, stdntMan.GetActiveStudentList(), "Name", "OBJID");
                pInterView = new InterView();
                if (stdntId != -1)
                {
                    drpStudent.SelectedValue = stdntId;
                    drpStudent.Enabled = false;
                    pInterView = new InterView() { StudentId = stdntId };
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmInterView.Load.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2012,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2012)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmInterView.Load.Error"),
                    messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = stdntId != -1;
            }
        }

        private void StudentChange(object sender, PositionChangedEventArgs e)
        {
            pInterView.StudentId = ConverterUtil.Convert2Int(drpStudent.SelectedValue);
            btnSave.Enabled = (pInterView.StudentId != 0);
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                pInterView.Comments = txtComments.Text;
                pInterView.Remarks = txtRemarks.Text;

                if (pInterView.StudentId == 0)
                {
                    ControlUtil.ShowWarningMessage(
                        messageRes.GetString("FrmInterView.Save.NoStudentSelected"),
                        messageRes.GetString("Warning"));
                    return;
                }
                else if (pInterView.Remarks.Trim().Length == 0)
                {
                    ControlUtil.ShowWarningMessage(
                      messageRes.GetString("FrmInterView.Save.NoRemarkSelected"),
                      messageRes.GetString("Warning"));
                    return;
                }
                else if (pInterView.Comments.Trim().Length == 0)
                {
                    ControlUtil.ShowWarningMessage(
                      messageRes.GetString("FrmInterView.Save.NoCommentSelected"),
                      messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    pInterView.UserId = AppVariables.CurrentUser.OBJID;
                    pInterView.InterViewTime = dtpTime.Value;
                    int intrVwId = intrVwMan.AddInterViewAndGetId(pInterView);
                    TransactionLogEntry transLogger = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = TransactionTypes.Insert,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag != null ? btnSave.Tag.ToString() : "",
                        LogObject = intrVwId
                    };
                    transLogger.Insert();
                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                        messageRes.GetString("FrmInterView.Save.Success"),
                        messageRes.GetString("Info"));
                }
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2013,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2013)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmInterView.Save.Error"),
                    messageRes.GetString("Error"));
            }
        }

        private void Cancel(object sedner, EventArgs e)
        {
            this.Close();
        }
    }
}
